<?php

namespace app\admin\service;

use app\admin\model\ApplyDeleteOpen;
use app\admin\model\BoxGame;
use app\channel\model\BoxGameOpen;
use think\Db;

class BoxApplyDeleteOpenService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new ApplyDeleteOpen();
    }

    public function getList()
    {
        $param = request()->param();
        $map   = [];
        //删档状态
        if (isset($param['status']) && !empty($param['status'])) {
            $map[] = ['status', '=', $param['status']];
        }
        //下单时间
        if (isset($param['create_time']) && !empty($param['create_time'])) {
            $map[] = ['create_time', 'between time', $param['create_time']];
        }
        $list = $this->model->where($map)->order("id DESC");
        $list = $list->paginate(10, false, ['query' => $param])->toArray();
        $arr  = $list['data'] ?? [];
        if (!empty($arr)) {
            $platData = Db::name('user_channel')->field(['id', 'nickname'])->select();
            $platData = array_column($platData, 'nickname', 'id');

            $openlist = BoxGameOpen::field(['id', 'name'])->select();
            $openlist = array_column($openlist ? $openlist->toArray() : [], null, 'id');

            $gamesList = BoxGame::field(['id', 'icon', 'game_name'])->select();
            $gamesList = array_column($gamesList ? $gamesList->toArray() : [], null, 'id');

            foreach ($arr as $key => &$value) {
                $value['plat_name']   = $platData[$value['plat_id']] ?? '';
                $value['game_info']   = $gamesList[$value['game_id']] ?? [];
                $value['open_name']   = $openlist[$value['open_id']]['name'] ?? '';
                $value['delete_time'] = $value['delete_time'] == '0000-00-00 00:00:00' ? '' : $value['delete_time'];
            }
        }

        //返回结果
        return array(
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $arr,
            "count" => $list['total'],
        );
    }

    public function review()
    {
        $param = request()->param();
        if (empty($param['is_audit'])) {
            return message("缺少参数", false);
        }
        if ($param['is_audit'] == '1') {
            $data['review_status'] = 2;
        } elseif ($param['is_audit'] == '2') {
            $data['review_status'] = 3;
            $data['reason']        = $param['reason'];
        }

        $this->model->update($data, ['id' => $param['id']]);

        return message();
    }
}
